﻿using System;
using System.Diagnostics;
using SpotOffer.AppConstants;
using SpotOffer.CustomViews;
using SpotOffer.DataModels;
using SpotOffer.Resource;
using SpotOffer.Utils;
using SpotOffer.View;
using SpotOffer.ViewModels;
using Xamarin.Forms;
using XLabs.Caching;
using XLabs.Forms.Mvvm;
using XLabs.Ioc;
using XLabs.Platform.Mvvm;

namespace SpotOffer
{
    public class App : Application
    {
        public static App AppInstance;
        public static ISimpleCache Cache;
        public App()
        {
            RegisterTypes();
            Cache = Resolver.Resolve<ISimpleCache>();
            Cache.FlushAll();
            AppInstance = this;
            MasterPage = new Root();
            MainPage = MasterPage;
            HandleSettings(null, null);
        }

        public Root MasterPage { get; set; }

        private void HandleSettings(object sender, EventArgs eventArgs)
        {
            if (String.IsNullOrEmpty(Cache.Get<string>(CacheKeys.Settings)))
            {
                Cache.Add(CacheKeys.Settings, CacheKeys.Internal);
            }
            if (String.IsNullOrEmpty(Cache.Get<string>(CacheKeys.PictureSettings)))
            {
                Cache.Add(CacheKeys.PictureSettings, CacheKeys.PicBig);
            }

            if (Cache.Get<City>(CacheKeys.City) == null)
            {
                 ((NavigationPage)AppInstance.MasterPage.Detail).PushAsync(ViewFactory.CreatePage<ChooseCityViewModel, ChooseCityPage>((v, p) =>
                {
                }) as Page, true).ConfigureAwait(true);
            }
            else
            {
                //MasterPageSpotOffer.IsPresented = true;
            }
        }

        public static void RegisterTypes()
        {
            ViewFactory.EnableCache = true;
            ViewFactory.Register<DealListPage, DealListViewModel>();
            ViewFactory.Register<ChooseCityPage, ChooseCityViewModel>();
            ViewFactory.Register<DealPage, DealViewModel>();
            ViewFactory.Register<SettingsPage, SettingsViewModel>();
            ViewFactory.Register<MenuPage, MenuViewModel>();
        }

        /// Initializes the application.
        /// </summary>
        public static void Init()
        {
            var app = Resolver.Resolve<IXFormsApp>();
            if (app == null)
            {
                return;
            }

            app.Closing += (o, e) => Debug.WriteLine("Application Closing");
            app.Error += (o, e) => Debug.WriteLine("Application Error");
            app.Initialize += (o, e) => Debug.WriteLine("Application Initialized");
            app.Resumed += (o, e) => Debug.WriteLine("Application Resumed");
            app.Rotation += (o, e) => Debug.WriteLine("Application Rotated");
            app.Startup += (o, e) => Debug.WriteLine("Application Startup");
            app.Suspended += (o, e) => Debug.WriteLine("Application Suspended");
        }
    }
}
